package com.yx.musicplayer.ui.fragment;

import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.yx.baselibrary.utils.GsonUtils;
import com.yx.baselibrary.utils.IntentUtil;
import com.yx.baselibrary.utils.SPUtil;
import com.yx.musicplayer.R;
import com.yx.musicplayer.bean.Album;
import com.yx.musicplayer.bean.Artist;
import com.yx.musicplayer.bean.FolderInfo;
import com.yx.musicplayer.constant.SPConstant;
import com.yx.musicplayer.greendao.Song;
import com.yx.musicplayer.ui.adapter.MusicAdapter;
import com.yx.musicplayer.ui.adapter.MusicItem;
import com.yx.musicplayer.ui.base.BaseFragment;
import com.yx.musicplayer.util.QuerySongUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by yx on 2017/11/1 0001.
 */

public class MusicFragment extends BaseFragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private int position;
    private List<MusicItem> musicItems;
    private MusicAdapter musicAdapter;


    @Override
    protected void onFirstUserVisible() {
        IntentUtil.startProgressDialog(getActivity());
        if (position == 0) {
            QuerySongUtil.queryAllSong(getActivity(), MediaStore.Audio.Media.DEFAULT_SORT_ORDER).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<Song>>() {
                @Override
                public void call(List<Song> songs) {
                    if (songs == null || songs.size() == 0) {
                        viewHelperUtil.toggleShowEmpty(true, "暂无数据", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                            }
                        });
                    } else {
                        SPUtil.putString(SPConstant.LAST_SONGS, GsonUtils.toJson(songs.get(0)));

                        for (Song song : songs) {
                            musicItems.add(position, new MusicItem(position, song));
                        }
                        musicAdapter.notifyDataSetChanged();
                        IntentUtil.stopProgressDialog();
                    }
                    IntentUtil.stopProgressDialog();
                }
            });
        } else if (position == 1) {
            QuerySongUtil.getAllArtists(getActivity()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<Artist>>() {
                @Override
                public void call(List<Artist> artists) {
                    if (artists == null || artists.size() == 0) {
                        viewHelperUtil.toggleShowEmpty(true, "暂无数据", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                            }
                        });
                    } else {
                        for (Artist artist : artists) {
                            MusicItem musicItem = new MusicItem();
                            musicItem.setItemType(position);
                            musicItem.setArtist(artist);
                            musicItems.add(musicItem);
                        }
                        musicAdapter.notifyDataSetChanged();
                    }
                    IntentUtil.stopProgressDialog();
                }
            });
        } else if (position == 2) {
            QuerySongUtil.getAllAlbums(getActivity()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<Album>>() {
                @Override
                public void call(List<Album> albums) {
                    if (albums == null || albums.size() == 0) {
                        viewHelperUtil.toggleShowEmpty(true, "暂无数据", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                            }
                        });
                    } else {
                        for (Album album : albums) {
                            MusicItem musicItem = new MusicItem();
                            musicItem.setItemType(position);
                            musicItem.setAlbum(album);
                            musicItems.add(musicItem);
                        }
                        musicAdapter.notifyDataSetChanged();
                    }
                    IntentUtil.stopProgressDialog();
                }
            });
        } else {
            QuerySongUtil.getFoldersWithSong(getActivity()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<FolderInfo>>() {
                @Override
                public void call(List<FolderInfo> folderInfos) {
                    if (folderInfos == null || folderInfos.size() == 0) {
                        viewHelperUtil.toggleShowEmpty(true, "暂无数据", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                            }
                        });
                    } else {
                        for (FolderInfo folderInfo : folderInfos) {
                            MusicItem musicItem = new MusicItem();
                            musicItem.setItemType(position);
                            musicItem.setFolderInfo(folderInfo);
                            musicItems.add(musicItem);
                        }
                    }
                    musicAdapter.notifyDataSetChanged();
                    IntentUtil.stopProgressDialog();
                }
            });
        }
    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    public void before(Bundle savedInstanceState) {
        position = getArguments().getInt("position", 0);
    }

    @Override
    public void initViewAndEvent() {
//        viewHelperUtil=new ViewHelperUtil(this);
        musicItems = new ArrayList<>();
        musicAdapter = new MusicAdapter(musicItems);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(musicAdapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (musicItems.get(position).getItemType()) {
                    case MusicItem.SPECIAL_SONG:
                        break;
                    default:
                        break;
                }
//                MusicPlayerServiceUtil.open();
            }
        });
    }

    @Override
    public int getContentViewLayoutID() {
        return R.layout.fragment_music;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public View getLoadingTargetView() {
        return recyclerView;
    }

    public static Fragment newInstance(int position) {
        MusicFragment musicFragment = new MusicFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        musicFragment.setArguments(bundle);
        return musicFragment;
    }
}
